package com.yjz.字节码学习;

import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtField;
import javassist.CtMethod;

import java.io.FileOutputStream;

/**
 * @author: yuanjinzhong
 * @date: 2023/12/18 17:36
 * @description:
 */
public class JavassistTest {

    public static void main(String[] args) {
//默认的类搜索路径
        ClassPool pool = ClassPool.getDefault();

        //获取一个ctClass对象
        CtClass ctClass = pool.makeClass("Test");
        try {
            //添加age属性
            ctClass.addField(CtField.make("private int age;", ctClass));
            //添加setAge方法
            ctClass.addMethod(CtMethod.make("public void setAge(int age){this.age = age;}", ctClass));
            //添加getAge方法
            ctClass.addMethod(CtMethod.make("public int getAge(){return this.age;}", ctClass));
            //将ctClass生成字节数组，并写入文件
            byte[] byteArray = ctClass.toBytecode();
            FileOutputStream output = new FileOutputStream("./Test.class");//根目录下生成
            output.write(byteArray);
            output.close();
            System.out.println("文件写入成功!!!");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
